package com.zsd.android.common

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.LinearInterpolator
import androidx.recyclerview.widget.RecyclerView

abstract class BaseRvAdapter<T>(mContext: Context, list: ArrayList<T>) : RecyclerView.Adapter<BaseViewHolder>() {

    protected var list: ArrayList<T>
    protected val mContext: Context
    protected val mInflater: LayoutInflater
    var onClickItemListener: OnClickItemListener? = null
    var headCount: Int = 0
    var footCount: Int = 0

    init {
        this.list = list
        this.mContext = mContext
        this.mInflater = LayoutInflater.from(mContext)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        return createHolder(parent, viewType)
    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        bindView(holder, position)
    }

    override fun getItemCount(): Int {
        return (if (list == null) 0 else list!!.size) + headCount + footCount
    }

    /**
     * 当Item出现在可视界面的时候调用
     * 可以用于设置每个Item的进入动画
     * 与之对应的还有onViewDetachedFromWindow
     * 这个方法是在Item离开界面的时候调用
     */
    override fun onViewAttachedToWindow(holder: BaseViewHolder) {
        super.onViewAttachedToWindow(holder)
        if (openAnim()) {
            //用于多个动画执行
            var scaleAnim = AnimatorSet()
            //X轴的缩放动画
            var scaleX = ObjectAnimator.ofFloat(holder.itemView, "scaleX", 0.9f, 1.0f)
            //Y轴的缩放动画
            var scaleY = ObjectAnimator.ofFloat(holder.itemView, "scaleY", 0.9f, 1.0f)
            //Y轴的平移动画
            var tranY = ObjectAnimator.ofFloat(holder.itemView, "translationY", 20.0f, 0f)
            //动画插值器，平稳执行
            scaleX.interpolator = LinearInterpolator()
            scaleY.interpolator = LinearInterpolator()
            tranY.interpolator = LinearInterpolator()
            //动画执行时间
            scaleAnim.duration = 300
            //三个动画一起执行
            scaleAnim.playTogether(scaleX, scaleY, tranY)
            scaleAnim.start()
        }
    }

    /**
     * 和上面的方法对应出现
     * 在Item离开界面的时候调用
     * 用来回收动画资源
     */
    override fun onViewDetachedFromWindow(holder: BaseViewHolder) {
        super.onViewDetachedFromWindow(holder)
    }

    /**
     * 添加所有数据
     *
     * @param collections 数据列表
     * @param isRefresh 是否刷新，默认刷新
     */
    fun addAll(collections: Collection<out T>, isRefresh: Boolean = true) {
        if (isRefresh) {
            list?.clear()
            list?.addAll(collections)
            notifyDataSetChanged()
        } else {
            var size = list?.size
            list?.addAll(collections)
            notifyItemRangeInserted(size, collections.size)
        }
    }

    /**
     * 添加单个数据，带动画
     *
     * @param data 数据
     * @param position 添加的位置，用于动画效果
     */
    fun add(data: T, position: Int) {
        list?.add(data)
        notifyItemInserted(position)
    }

    /**
     * 移除单个数据，带动画
     *
     * @param data 数据
     * @param position 移除的位置，用于动画效果
     */
    fun remove(data: T, position: Int) {
        list?.remove(data)
        notifyItemRemoved(position)
    }

    abstract fun createHolder(parent: ViewGroup, viewType: Int): BaseViewHolder

    abstract fun bindView(holder: BaseViewHolder, position: Int)

    open fun openAnim(): Boolean {
        return false
    }

    interface OnClickItemListener {

        fun onClickItem(adapter: BaseRvAdapter<*>, view: View, position: Int, tag: Int)
    }
}